package com.pipodi.itasamobile.connections;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class Connection {
		
	public Connection(){
		super();
	}
	
	public static Document connectToAPIUrl(String url){
		URL apiURL;
		try {
			apiURL = new URL(url);
			HttpURLConnection connection = (HttpURLConnection) apiURL.openConnection(); 
			connection.setRequestMethod("GET");
			connection.setRequestProperty("User-Agent", "ItaSA Mobile Test Application by Pipodi");
			InputStream stream = connection.getInputStream(); 
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(stream);
			doc.getDocumentElement().normalize();
			connection.disconnect();
			return doc;
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static void loginConnection(){
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost("http://www.italiansubs.net/index.php");
		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
		post.setHeader("User-Agent","myItaSA Application Test by Pipodi");
	    post.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		pairs.add(new BasicNameValuePair("username", "Pipodi"));
		pairs.add(new BasicNameValuePair("passwd", "140507"));
		pairs.add(new BasicNameValuePair("remember", "yes"));
		pairs.add(new BasicNameValuePair("option", "com_user"));
		pairs.add(new BasicNameValuePair("task", "login"));
		pairs.add(new BasicNameValuePair("silent", "true"));
		pairs.add(new BasicNameValuePair("return", "aHR0cDovL3d3dy5pdGFsaWFuc3Vicy5uZXQv"));
		try {
			post.setEntity(new UrlEncodedFormEntity(pairs));
			HttpResponse response = client.execute(post);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
